package com.waimai.demo.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.waimai.demo.Service.CategoryService;
import com.waimai.demo.Service.SetmealDishService;
import com.waimai.demo.Service.SetmealService;
import com.waimai.demo.back.R;
import com.waimai.demo.domain.Category;
import com.waimai.demo.domain.Dish;
import com.waimai.demo.domain.Setmeal;
import com.waimai.demo.domain.SetmealDish;
import com.waimai.demo.dto.SetmealDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    SetmealService setmealService;
    @Autowired
    CategoryService categoryService;
    @Autowired
    SetmealDishService setmealDishService;

    @PostMapping
    public R<String> sava(@RequestBody SetmealDto setmealDto, HttpServletRequest request) {
        log.info(setmealDto.toString() + "===============");
        setmealService.savawithDish(setmealDto, request);
        return R.success("保存套餐成功");
    }

    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
        List<SetmealDto> setmealDtoList = new ArrayList<>();
        Page<Setmeal> page1 = new Page(page, pageSize);
        Page<SetmealDto> pageInfo = new Page<>();
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.like(name != null, Setmeal::getName, name);
        setmealService.page(page1, lambdaQueryWrapper);
        BeanUtils.copyProperties(page1, pageInfo, "records");
        List<Setmeal> records = page1.getRecords();
        for (Setmeal record : records) {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(record, setmealDto);
            Category category = categoryService.getById(record.getCategoryId());
            setmealDto.setCategoryName(category.getName());

            setmealDtoList.add(setmealDto);
        }
        pageInfo.setRecords(setmealDtoList);
        return R.success(pageInfo);
    }

    @DeleteMapping
    public R<String> remove(Long ids) {
        Setmeal setmeal = setmealService.getById(ids);
        LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(SetmealDish::getSetmealId, setmeal.getId());
        setmealDishService.remove(lambdaQueryWrapper);
        setmealService.removeById(ids);
        return R.success("删除成功");


    }

    @GetMapping("/{id}")
    public R<SetmealDto> update(@PathVariable Long id) {
        Setmeal setmeal = setmealService.getById(id);
        SetmealDto setmealDto = new SetmealDto();
        LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> list = setmealDishService.list(lambdaQueryWrapper);
        BeanUtils.copyProperties(setmeal, setmealDto);
        setmealDto.setSetmealDishes(list);
        return R.success(setmealDto);
    }

    @PutMapping
    public R<String> updatewithsava(@RequestBody SetmealDto setmealDto, HttpServletRequest req) {
        setmealDto.setCreateTime(LocalDateTime.now());
        setmealDto.setUpdateTime(LocalDateTime.now());
        Long id = (Long) req.getSession().getAttribute("employee");
        setmealDto.setCreateUser(id);
        setmealDto.setUpdateUser(id);
        setmealService.updateById(setmealDto);
        LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(SetmealDish::getSetmealId, setmealDto.getId());
        setmealDishService.remove(lambdaQueryWrapper);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDto.getId());
            setmealDish.setCreateTime(LocalDateTime.now());
            setmealDish.setUpdateTime(LocalDateTime.now());
            setmealDish.setCreateUser(id);
            setmealDish.setUpdateUser(id);
        }
        setmealDishService.saveBatch(setmealDishes);
        return R.success("修改成功");
    }

    @Cacheable
    @GetMapping("/list")
    public R<List> huixian(Setmeal setmeal) {
        log.info("===================" + setmeal.getCategoryId());
        Long id = setmeal.getCategoryId();
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Setmeal::getCategoryId, id);
        List<Setmeal> list = setmealService.list(lambdaQueryWrapper);

        return R.success(list);
    }
}
